/*   1:    */ package org.jetbrains.rsa;
/*   2:    */ 
/*   3:    */

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.util.Map;

/*   4:    */
/*   5:    */
/*   6:    */
/*   7:    */ 
/*   8:    */ public class RSAEncoder
/*   9:    */ {
/*  10:    */   private final BigInteger a;
/*  11:    */   private final BigInteger b;
/*  12:    */   private final int c;
/*  13:    */   private final int d;
/*  14: 15 */   private int e = 0;
/*  15:    */   private final BigInteger f;
/*  16:    */   private final boolean g;
/*  17:    */   
/*  18:    */   public RSAEncoder(BigInteger paramBigInteger1, BigInteger paramBigInteger2)
/*  19:    */   {
/*  20: 20 */     this(paramBigInteger1, paramBigInteger2, 36, true);
/*  21:    */   }
/*  22:    */   
/*  23:    */   public RSAEncoder(BigInteger paramBigInteger1, BigInteger paramBigInteger2, int paramInt, boolean paramBoolean)
/*  24:    */   {
/*  25: 26 */     this.a = paramBigInteger1;
/*  26: 27 */     this.b = paramBigInteger2;
/*  27: 28 */     this.g = paramBoolean;
/*  28: 29 */     int j = paramBigInteger1.bitLength();
/*  29: 30 */     this.f = new BigInteger(String.valueOf(paramInt));
/*  30: 31 */     int i = (int)Math.ceil(j / Math.log(paramInt) * Math.log(2.0D));
/*  31: 32 */     if (i % 5 != 0) {
/*  32: 33 */       i = (i / 5 + 1) * 5;
/*  33:    */     }
/*  34: 35 */     this.d = i;
/*  35: 36 */     this.c = (j / 8 - 1);
/*  36:    */   }
/*  37:    */   
/*  38:    */   public String encode(byte[] paramArrayOfByte)
/*  39:    */   {
/*  40: 40 */     int i = paramArrayOfByte.length % this.c;
/*  41: 41 */     byte[] arrayOfByte = new byte[i == 0 ? paramArrayOfByte.length : paramArrayOfByte.length + this.c - i];
/*  42: 42 */     System.arraycopy(paramArrayOfByte, 0, arrayOfByte, this.c - i, paramArrayOfByte.length);
/*  43:    */     
/*  44: 44 */     StringBuffer localStringBuffer = new StringBuffer();
/*  45: 45 */     for (int j = 0; j < arrayOfByte.length; j += this.c) {
/*  46: 46 */       a(arrayOfByte, localStringBuffer, j, this.c);
/*  47:    */     }
/*  48: 49 */     return localStringBuffer.toString();
/*  49:    */   }
/*  50:    */   
/*  51:    */   public String encodeString(String paramString)
/*  52:    */   {
/*  53:    */     try
/*  54:    */     {
/*  55: 54 */       return encode(paramString.getBytes("UTF-8"));
/*  56:    */     }
/*  57:    */     catch (UnsupportedEncodingException localUnsupportedEncodingException) {}
/*  58: 58 */     return "";
/*  59:    */   }
/*  60:    */   
/*  61:    */   private void a(byte[] paramArrayOfByte, StringBuffer paramStringBuffer, int paramInt1, int paramInt2)
/*  62:    */   {
/*  63: 63 */     if (paramInt2 == 0) {
/*  64: 63 */       return;
/*  65:    */     }
/*  66: 64 */     byte[] arrayOfByte = new byte[this.c];
/*  67: 65 */     System.arraycopy(paramArrayOfByte, paramInt1, arrayOfByte, 0, paramInt2);
/*  68: 66 */     BigInteger localBigInteger1 = new BigInteger(1, arrayOfByte);
/*  69: 67 */     if (localBigInteger1.compareTo(this.b) >= 0) {
/*  70: 68 */       throw new IllegalArgumentException("result is too long");
/*  71:    */     }
/*  72: 71 */     BigInteger localBigInteger2 = localBigInteger1.modPow(this.a, this.b);
/*  73: 72 */     paramStringBuffer.append(a(a(localBigInteger2)));
/*  74:    */   }
/*  75:    */   
/*  76:    */   private String a(String paramString)
/*  77:    */   {
/*  78: 76 */     StringBuffer localStringBuffer = new StringBuffer();
/*  79: 77 */     for (int i = 0; i < paramString.length(); i++)
/*  80:    */     {
/*  81: 78 */       a(localStringBuffer);
/*  82: 79 */       localStringBuffer.append(paramString.charAt(i));
/*  83:    */     }
/*  84: 81 */     return localStringBuffer.toString();
/*  85:    */   }
/*  86:    */   
/*  87:    */   private String a(BigInteger paramBigInteger)
/*  88:    */   {
/*  89: 85 */     StringBuffer localStringBuffer = new StringBuffer();
/*  90: 86 */     for (int i = 0; i < this.d; i++)
/*  91:    */     {
/*  92: 87 */       localStringBuffer.insert(0, b(paramBigInteger.mod(this.f)));
/*  93: 88 */       paramBigInteger = paramBigInteger.divide(this.f);
/*  94:    */     }
/*  95: 90 */     return localStringBuffer.toString();
/*  96:    */   }
/*  97:    */   
/*  98:    */   private void a(StringBuffer paramStringBuffer)
/*  99:    */   {
/* 100: 94 */     if ((this.e > 0) && (this.e % 5 == 0)) {
/* 101: 95 */       if (this.e % 30 == 0) {
/* 102: 96 */         paramStringBuffer.append('\n');
/* 103: 97 */       } else if (this.g) {
/* 104: 98 */         paramStringBuffer.append('-');
/* 105:    */       }
/* 106:    */     }
/* 107:101 */     this.e += 1;
/* 108:    */   }
/* 109:    */   
/* 110:    */   private static char b(BigInteger paramBigInteger)
/* 111:    */   {
/* 112:105 */     int i = paramBigInteger.intValue();
/* 113:    */     char c1;
/* 114:107 */     if (i < 10) {
/* 115:108 */       c1 = (char)(48 + i);
/* 116:109 */     } else if (i < 36) {
/* 117:110 */       c1 = (char)(65 + i - 10);
/* 118:111 */     } else if (i < 62) {
/* 119:112 */       c1 = (char)(97 + i - 36);
/* 120:    */     } else {
/* 121:114 */       c1 = (char)(33 + i - 62);
/* 122:    */     }
/* 123:117 */     return c1;
/* 124:    */   }
/* 125:    */   
/* 126:    */   public String encodeProperties(Map<String, String> paramMap)
/* 127:    */   {
/* 128:121 */     StringBuffer localStringBuffer = new StringBuffer();
/* 129:122 */     for (Map.Entry localEntry : paramMap.entrySet())
/* 130:    */     {
/* 131:123 */       String str1 = (String)localEntry.getKey();
/* 132:124 */       String str2 = (String)localEntry.getValue();
/* 133:126 */       if ((str1.indexOf('\n') >= 0) || (str2.indexOf('\n') >= 0)) {
/* 134:127 */         throw new RuntimeException("Keys and values are not allowed to contain linefeeds");
/* 135:    */       }
/* 136:130 */       localStringBuffer.append(str1).append('\n').append(str2).append('\n');
/* 137:    */     }
/* 138:133 */     return encodeString(localStringBuffer.toString());
/* 139:    */   }
/* 140:    */ }



/* Location:           E:\idea\

 * Qualified Name:     org.jetbrains.rsa.RSAEncoder

 * JD-Core Version:    0.7.0.1

 */